<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
  <head>
    <base href="<%=basePath%>">
	<%@ include file="/base/jsp/include.jsp"%>
	<script type="text/javascripthttp://dns.www.net.cn/static/images/loading.gif">
	</script>
  </head>
  <body>
  <div class="mini-toolbar" style="padding:2px;border-bottom:0;">
		<table style="width:100%;">
			<tr>
				<td style="width:100%;"><a class="mini-button"
					iconCls="icon-add" plain="true" onclick="newRow()">添加</a></td>
			</tr>
		</table>
	</div>
    <div id="datagrid1" class="mini-datagrid" style="width:100%;height:auto;" allowResize="true"
        url="listDepartment.do"  idField="id">
        <div property="columns">
            <div name="action" width="120" headerAlign="center" align="center" renderer="onActionRenderer" cellStyle="padding:0;">操作</div>
			<div type="indexcolumn" headerAlign="center">序号</div>
            <div field="name" width="120" headerAlign="center" allowSort="true">名称</div>                           
            <div field="code" width="100" allowSort="true">代码</div>          
        </div>
    </div>  
    <div id="editForm1" style="display:none;padding:5px;position:relative;">
    <input class="mini-hidden" name="id"/>
    <table style="width:100%;">
        <tr>
            <td style="width:80px;">名称：</td>
            <td style="width:150px;"><input name="name" class="mini-textbox" required="true" /></td>
            <td style="width:150px; display: none;"><input name="pid" class="mini-textbox" required="true" /></td>
        </tr>
        <tr>
            <td style="text-align:right;padding-top:5px;padding-right:20px;" colspan="6">
                <a class="mini-button" href="javascript:updateRow();">确定</a> 
                <a class="mini-button" href="javascript:cancelRow();">取消</a>
            </td>                
        </tr>
    </table>
</div>
    <script type="text/javascript">
    	///////////////////////////////////////////////////////       
		var pid = '';
		mini.parse();
		var grid = mini.get("datagrid1");
		var editForm = document.getElementById("editForm1");  
        var form = new mini.Form("form");
		function SetData(data) {
			pid = data.id;
			grid.load({pid : pid});
		}
		
        function onActionRenderer(e) {
            var grid = e.sender;
            var record = e.record;
            var uid = record._uid;
            var rowIndex = e.rowIndex;

            var s = '<a class="New_Button" href="javascript:newRow()">添加</a> '
                    + '<a class="New_Button" href="javascript:editRow(\'' + uid + '\')">编辑</a> '
                    + '<a class="New_Button" href="javascript:delRow(\'' + uid + '\')">删除</a> '
                    + '<a class="New_Button" href="javascript:editMajor(\'' + uid + '\')">编辑专业</a> ';
                       
            return s;
        }

        function newRow() {            
            var row = {};
            grid.addRow(row, 0);

            editRow(row._uid);
        }
        function editRow(row_uid) {
            var row = grid.getRowByUID(row_uid);
            if (row) {
                //显示行详细
                grid.hideAllRowDetail();
                grid.showRowDetail(row);

                //将editForm元素，加入行详细单元格内
                var td = grid.getRowDetailCellEl(row);
                td.appendChild(editForm);
                editForm.style.display = "";

                //表单加载投票项信息
                var form = new mini.Form("editForm1");
                if (grid.isNewRow(row)) {                    
                    form.reset();
                } else {
                    form.loading();
                    $.ajax({
                        url: "findByIdDepartment.do?id=" + row.id,
                        success: function (text) {
                            var o = mini.decode(text);
                            form.setData(o);                            

                            form.unmask();
                        }
                    });
                }

                grid.doLayout();
        
            }
        }
        function cancelRow() {
            grid.reload({pid : pid});
        }
        function delRow(row_uid) {
            var row = grid.getRowByUID(row_uid);
            if (row) {
                alert(row.id);
                if (confirm("确定删除此记录？")) {
                    grid.loading("删除中，请稍后......");
                    $.ajax({
                        url: "delDepartment.do?id=" + row.id,
                        success: function (text) {
                            grid.load({pid : pid});
                        },
                        error: function () {
                        }
                    });
                }
            }
        }

        function updateRow() {
            var form = new mini.Form("editForm1");
            var o = form.getData();
            form.validate();
            grid.loading("保存中，请稍后......");
            o.pid = pid;
            var json = mini.encode(o);
            $.ajax({
                url: "saveDepartment.do",
                type: 'post',
                data: $.evalJSON(json),
                cache: false,
                success: function (text) {
                    grid.load({pid : pid});
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                }
            });

        }
        
        function editMajor() {
		var rows = grid.getSelecteds();
		if (rows.length == 1) {
			var row = grid.getSelected();
			mini.open({
				url : "toEditMajor.do",
				title : row.name + "专业编辑",
				width : 800,
				height : 500,
				onload : function() {
					var iframe = this.getIFrameEl();
					var data = {
						action : "edit",
						id : row.id
					};
					iframe.contentWindow.SetData(data);
				},
				ondestroy : function(action) {
					grid.reload();
				}
			});
		} else if (rows.length > 1) {
			mini.alert("只能选一条记录");
		} else {
			mini.alert("请选中一条记录");
		}

	}
    </script>	
  </body>
</html>
